package flc.ast.fragment.edit;

import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.imgedit.ImgEditActivity;
import com.stark.imgedit.ModuleConfig;
import com.stark.imgedit.fliter.PhotoProcessing;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseEditFragment;
import flc.ast.R;
import flc.ast.adapter.PictureFilterAdapter;
import flc.ast.bean.MyPictureFilterBean;
import flc.ast.databinding.FragmentFilterBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.RxUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FilterFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FilterFragment extends BaseEditFragment<FragmentFilterBinding> {

    public static final int INDEX = ModuleConfig.INDEX_FILTER;
    private PictureFilterAdapter filterAdapter;
    private int oldPosition;
    private Bitmap mImgBitmap, mFilterBitmap;

    public static FilterFragment newInstance() {
        FilterFragment fragment = new FilterFragment();
        return fragment;
    }

    @Override
    public void onShow() {
        mImgEditActivity.mode = ImgEditActivity.MODE_FILTER;
        mImgBitmap = mImgEditActivity.getMainBit();
        if (filterAdapter != null) {
            filterAdapter.getItem(oldPosition).setSelect(false);
            filterAdapter.getItem(0).setSelect(true);
            oldPosition = 0;
            filterAdapter.notifyDataSetChanged();
        }
        mImgEditActivity.mImgView.setImageBitmap(mImgEditActivity.getMainBit());
    }

    @Override
    public void backToMain() {
        mImgEditActivity.mode = ImgEditActivity.MODE_NONE;
        mImgEditActivity.mImgView.setImageBitmap(mImgEditActivity.getMainBit());
        mImgEditActivity.showFunc();
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_filter;
    }

    @Override
    protected void initView() {

        mDataBinding.rvPicFilterList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        filterAdapter = new PictureFilterAdapter();
        mDataBinding.rvPicFilterList.setAdapter(filterAdapter);
        filterAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {

    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        filterAdapter.getItem(oldPosition).setSelect(false);
        filterAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        filterAdapter.getItem(position).setSelect(true);
        filterAdapter.notifyItemChanged(position);
        setFilter(position);
    }

    @Override
    protected void initData() {
        //滤镜数据
        String[] mFilters = getResources().getStringArray(com.stark.imgedit.R.array.filters);
        List<MyPictureFilterBean> listFilter = new ArrayList<>();
        listFilter.add(new MyPictureFilterBean(mFilters[0], "#00000000", true));
        listFilter.add(new MyPictureFilterBean(mFilters[1], "#323333", false));
        listFilter.add(new MyPictureFilterBean(mFilters[2], "#C376F3", false));
        listFilter.add(new MyPictureFilterBean(mFilters[3], "#6F7171", false));
        listFilter.add(new MyPictureFilterBean(mFilters[4], "#505151", false));
        listFilter.add(new MyPictureFilterBean(mFilters[5], "#999999", false));
        listFilter.add(new MyPictureFilterBean(mFilters[6], "#513232", false));
        listFilter.add(new MyPictureFilterBean(mFilters[7], "#787A7A", false));
        listFilter.add(new MyPictureFilterBean(mFilters[8], "#5000FF00", false));
        listFilter.add(new MyPictureFilterBean(mFilters[9], "#500000FF", false));
        listFilter.add(new MyPictureFilterBean(mFilters[10], "#50FF0000", false));
        listFilter.add(new MyPictureFilterBean(mFilters[11], "#50FFFF00", false));
        filterAdapter.setList(listFilter);
        onShow();
    }

    //设置滤镜
    private void setFilter(final int f) {
        showDialog(getString(R.string.set_filter_ing));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                Bitmap retBitmap = Bitmap.createBitmap(mImgBitmap.copy(
                        Bitmap.Config.ARGB_8888, true));
                PhotoProcessing.filterPhoto(retBitmap, f);
                emitter.onNext(retBitmap);
            }

            @Override
            public void accept(Bitmap bitmap) {
                dismissDialog();
                if (bitmap == null) return;
                mFilterBitmap = bitmap;
                mImgEditActivity.mImgView.setImageBitmap(mFilterBitmap);
            }
        });
    }

    /**
     * 保存滤镜图片
     */
    public void applyFilterImage() {
        mImgEditActivity.changeMainBitmap(mFilterBitmap, true);
        backToMain();
    }
}